package utils;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class DataParse {
	/*
	 * public static void parseHtml(String html) { Document doc =
	 * Jsoup.parse(html, "gbk"); Elements links = doc.getElementsByTag("a"); for
	 * (Element link : links) { String linkClick = link.attr("onclick"); String
	 * linkHref = link.attr("href"); String linkText = link.text();
	 * System.out.println(linkText); } }
	 */

	/**
	 * 解析script
	 *
	 * @param html
	 * @return
	 */
	public static List<SelectOptionContent> parseScriptOption(String html, String name) {
		Document doc = Jsoup.parse(html, "gbk");
		Elements links = doc.getElementsByTag("script");
		return parseHTMLOption(links.html(), name);
	}

	/**
	 * 选取需要解析的部分
	 *
	 * @param html
	 * @param name
	 * @return
	 */
	private static String selectNeedHTML(String html, String name) {
		if (name == null || name.equals("")) {
			return html;
		}
		Document doc = Jsoup.parse(html, "gbk");
		Elements links = doc.getElementsByTag("select");
		for (Element element : links) {
			String selectContent = element.attr("name");
			if (selectContent.equals(name)) {
				return element.html();
			}
		}
		return "";
	}

	/**
	 * 解析option
	 *
	 * @param html
	 * @return
	 */
	public static List<SelectOptionContent> parseHTMLOption(String html, String name) {
		html = selectNeedHTML(html, name);
		Document doc = Jsoup.parse(html, "gbk");
		Elements links = doc.getElementsByTag("option");
		List<SelectOptionContent> spinnerContents = new ArrayList<SelectOptionContent>();
		for (Element element : links) {
			spinnerContents.add(new SelectOptionContent(element.attr("value"), element.text()));
		}
		return spinnerContents;
	}

	/**
	 * 判断验证码
	 *
	 * @param html
	 * @return
	 */
	public static boolean parseCheckVerCode(String html) {
		if (html == null) {
			return false;
		}
		Document doc = Jsoup.parse(html, "gbk");
		Elements links = doc.getElementsByTag("script");
		for (Element elScripts : links) {
			if (elScripts.html().contains("验证码错误")) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 解析课表
	 *
	 * @param html
	 * @return
	 */
	public static List<String> parseCourse(String html) {
		List<String> contents = new ArrayList<String>();
		Document doc = Jsoup.parse(html, "utf-8");
		Elements links = doc.select("tbody");
		for (Element element : links) {
			doc = Jsoup.parse("<table>" + element.html() + "</table>", "utf-8");
			Elements elsTr = doc.select("tr");
			for (int i = 0; i < elsTr.size(); i++) {
				doc = Jsoup.parse("<table>" + elsTr.get(i).html() + "</table>", "utf-8");
				Elements elsTd = doc.select("td");
				for (int j = 0; j < elsTd.size(); j++) {
					String content = elsTd.get(j).text();
					contents.add(content);
				}
			}
		}
		return contents;
	}

	/**
	 * 解析课程数据
	 * @param html
	 * @return
	 */
	public static String parseCourseToTable(String html) {
		Document doc = Jsoup.parse(html, "utf-8");
		Elements links = doc.select("div");
		String data = links.html();
		int subIndex = html.lastIndexOf("</table>");
		if (data.trim().equals("") && subIndex != -1) {
			data = html.substring(0, subIndex);
		}
		
		return data;
	}

}
